function Grid($,pcCommon){
	
	/*
		container: //Grid容器
		withListTemp:false,
		listContainer:

		
		list:       //数据列表
		url:        //ajax请求url
			type:       //get,post(默认get) url指定时需要用到
			params:  //funciton(){} || {} 两种方式
		getList:function(){}
		
		columns:   {name:名称,key:字段,format:function(){},style=""样式}
		genTable:function(){}


		pageInfo:  {all:所有页数,page:当前页,pagesize:每页多少条}
		empty:     function(){} 为空时需要回调处理
		
		dispNumCnt           // 显示Number数（默认6）
		withJump             // 跳转模块(默认有)
		withQuickKey         // 快捷键（默认有）
		schema               // Table,List
		
		PS:提示
		container：为必须项
		
		顺序 list > getList > url
		
	 */
	var _DEF_OPTIONS = {type:'get',withListTemp:false,dispNumCnt:6,withJump:true,withQuickKey:true,schema:'table'};
	return {
		init:function(options){
			this.settings = $.extend({},_DEF_OPTIONS,options);
			
			if(!this.check()){ // 参数校验
				return;
			}

			this.isTableList = true;
			if(this.settings.schema === "table"){
				this.gridTableSchema(); // 创建grid的结构
			}else{
				this.gridListSchema();  // 创建List的结构
				this.isTableList = false;
			}

			this.dispMask();

			this.tabler = GridTable($,this);
			this.pager =  GridPager($,this);

			this.pageInfo = $.extend({},{page:1,pagesize:10},this.settings.pageInfo);

			this.pageInfo.dispNumCnt = this.settings.dispNumCnt;
			this.pageInfo.withJump = this.settings.withJump;
			this.pageInfo.withQuickKey = this.settings.withQuickKey;

			this.gen(); // 初期生成
			
			return this;
		},
		
		gen:function(){
			var _this = this;
			var tbDatas = null;
			if(_this.settings.list && _this.settings.list.length > 0){ // 固定list
				_this.pageInfo.all = _this.settings.list.length;
				if(_this.pageInfo.page * _this.pageInfo.pagesize > _this.pageInfo.all){
					_this.pageInfo.page = 1;
				}
				tbDatas = _this.getDataFromList(_this.pageInfo, _this.settings.list);
				genTb(tbDatas);
			}
			else if(_this.settings.getList){
				_this.settings.getList.call(null,_this.pageInfo.page,function(tbDatas){
					_this.pageInfo = $.extend({},_this.pageInfo,{page:tbDatas.data.pageNum,pagesize:tbDatas.data.pageSize,all:tbDatas.data.total});
					tbDatas = tbDatas.data.list;
					genTb(tbDatas);
				});
			}
			else{
				var params = {};
				if(_this.settings.params){
					if(_this.settings.params instanceof Function)
						params = _this.settings.params.call();
					else
						params = _this.settings.params;
				}
				params.page = params.page?params.page:_this.pageInfo.page;
				var type = _this.settings.type?_this.settings.type:'get';
				pcCommon.ajax({
					url:_this.settings.url,
					type:type,
					data:params,
					dataType:'json',
					success:function(data){
						var pgListInfo = data.data;
						_this.pageInfo = $.extend({},_this.pageInfo,{page:pgListInfo.pageNum,pagesize:pgListInfo.pageSize,all:pgListInfo.total});
						tbDatas = pgListInfo.list;
						genTb(tbDatas);
					}
				});
			}

			function genTb(tbDatas){
				if(_this.pageInfo.page == 1 && (!tbDatas|| tbDatas.length == 0)){
					_this.settings.empty?_this.settings.empty.call():void(0);
					return;
				}
				if(_this.isTableList){
					if(_this.settings.columns){
						_this.genTbHeader(_this.settings.columns);
						_this.genTbRows(tbDatas,_this.settings.columns);
						_this.genTbPager();
					}else if(_this.settings.genTable){
						_this.settings.genTable(tbDatas,_this.tbListHeader,_this.tbListBody);
						_this.genTbPager();
					}
				}else{
					_this.settings.genTable(tbDatas,_this.tbListOnly);
					_this.genTbPager();
				}
				_this.hideMask();
			}
		},
		genTbHeader:function(columns){
			var _this = this;
			_this.tabler.header(columns);
		},
		genTbRows:function(data,columns){
			var _this = this;
			_this.tabler.body(data,columns);
		},
		genTbPager:function(){
			var _this = this;
			_this.pager.gen(_this.pageInfo);
		},
		refresh:function(){
			var _this = this;
			_this.pageInfo.page = 1;
			_this.gen();
		},
		on_pager_change:function(pageNo){
			var _this = this;
			try{
				pageNo = parseInt(pageNo);
				_this.pageInfo.page = pageNo;
				_this.gen();
			}catch(e){

			}
		},
		check:function(){
			var _this = this,flg = true;

			if(!_this.settings.container ){
				pcCommon.logger("container 没有指定");
				flg = false;
			}

			if(_this.settings.withListTemp && !_this.settings.listContainer){
				pcCommon.logger("list 模板存在时，必须设置 listContainer");
				flg = false;
			}

			if(_this.settings.withListTemp && _this.settings.listContainer && !_this.settings.genTable)
			{
				pcCommon.logger("list 模板存在时，必须要指定genTable");
				flg = false;
			}

			if(!_this.settings.list && !_this.settings.url &&  !_this.settings.getList){
				pcCommon.logger("list 和 url 和 getList 都没有指定");
				flg = false;	
			}

			if(_this.settings.list && _this.settings.list.length == 0){
				pcCommon.logger("list 指定了且长度为0");
				flg = false;
			}

			if(!_this.settings.columns && !_this.settings.genTable)
			{
				pcCommon.logger("生成方式必须指定");
				flg = false;
			}

			if(!flg){
				if(_this.settings.empty){
					_this.hideMask();
					_this.settings.empty.call();//回调空函数
				}
			}
			
			return flg;
		},
		gridListSchema:function(){
			var _this = this;
			var gridCon = $(_this.settings.container);

			// mask
			var mask = gridCon.find(".grid-mask");
			if( mask.length == 0){
				mask = $('<div class="grid-mask"></div>');
				_this.gridMask = mask;
				gridCon.append(mask);
			}

			// table
			var tbCon = gridCon.find(".grid-table");
			if( tbCon.length == 0)
			{
				var tbCon = $('<div class="grid-table"></div>');
				_this.gridTb = tbCon;
				gridCon.append(tbCon);
				_this.gridTb.html("");
			}
			

			
			// table - list
			if(_this.settings.withListTemp)
			{
				_this.tbListOnly = _this.settings.listContainer;
			}
			else
			{
				var tbList = $('<ul class="grid-list list"></ul>');
				_this.tbListOnly = tbList;
				_this.gridTb.append(tbList);
			}
			
			// pager
			var pager = $('<div class="grid-pager"></div>');
			_this.tbpager = pager;
			gridCon.append(_this.tbpager);

		},
		gridTableSchema:function(){
			var _this = this;
			var gridCon = $(_this.settings.container);

			// mask
			var mask = gridCon.find(".grid-mask");
			if( mask.length == 0){
				mask = $('<div class="grid-mask"></div>');
				_this.gridMask = mask;
				gridCon.append(mask);
			}

			// table
			var tbCon = gridCon.find(".grid-table");
			if( tbCon.length == 0)
			{
				var tbCon = $('<div class="grid-table"></div>');
				_this.gridTb = tbCon;
				gridCon.append(tbCon);
			}
			_this.gridTb.html("");

			// table - list
			if(_this.settings.withListTemp)
			{
				if(_this.settings.listContainer.find("thead").length > 0){
					_this.tbListHeader = _this.settings.listContainer.find("thead")[0];
				}
				else
				{
					_this.tbListHeader = $('<thead>');
					_this.settings.listContainer.append(_this.tbListHeader);
				}
				if(_this.settings.listContainer.find("tbody").length > 0){
					_this.tbListBody = _this.settings.listContainer.find("tbody")[0];
				}
				else
				{
					_this.tbListBody = $('<tbody>');
					_this.settings.listContainer.append(_this.tbListBody);
				}
			}
			else
			{
				var tbList = $('<table class="grid-list" cellspacing="0" cellpadding="0"></table>');
				var tbListHeader = $('<thead>');
				_this.tbListHeader = tbListHeader;
				var tbListBody = $('<tbody>');
				_this.tbListBody = tbListBody;
				tbList.append(tbListHeader);
				tbList.append(tbListBody);
				_this.gridTb.append(tbList);
			}

			// pager
			var pager = $('<div class="grid-pager"></div>');
			_this.tbpager = pager;
			gridCon.append(_this.tbpager);
		},

		getDataFromList:function(pageInfo,dataList){
			var _this = this;
			var startIdx = (pageInfo.page - 1) * pageInfo.pagesize;//数组重0开始
			startIdx = startIdx > 0?(startIdx - 1):0;
			var endIdx = startIdx + pageInfo.pagesize;
			var tbDatas = [];
			for(var i=startIdx; i<endIdx; i++){
				var rowData = dataList[i];
				if(rowData){
					tbDatas.push(dataList[i]); 
				}
			}
			return tbDatas;
		},

		dispMask:function(){
			this.gridMask?this.gridMask.removeClass("hide"):void(0);
		},

		hideMask:function(){
			this.gridMask?this.gridMask.addClass("hide"):void(0);
		}
	}
}

function GridTable($,Grid){
	return {
		init:function(){
			return this;
		},
		header:function(columns){
			if(Grid.tbListHeader.children().length == 0){
				var str =  '<tr class="grid-list-header">';
				$(columns).each(function(){
					if(this.key){
						var d = '<th style="' + (this.style?this.style:"") + '">' ;
						str += ('<th style="' + (this.style?this.style:"") + '">' + (this.name?this.name:this.key) + '</th>');
					}
				});
				str += '</tr>';
				Grid.tbListHeader.html(str);
			}
		},
		body:function(datas,columns){
			var str = "",_this = this;
			$(datas).each(function(){
				str += '<tr>';
				var tmpData = this;
				$(columns).each(function(){
					str += '<td>';
					if(this.format){
						str += this.format.call(null,tmpData);
					}else{
						str += tmpData[this.key];
					}
					
					str += '</td>';
				});
				str += '</tr>';
			});
			Grid.tbListBody.html(str);
			Grid.hideMask();
		}
	}
}

function GridPager($,Grid){
	return {
		init:function(){
			
			return this;
		},
		schema:function(){
			var _this = this;

			_this.pagerCon = $('<ul class="pager-container"></ul>');
			_this.pagerSt = $('<li class="pager-item" tg="start" ><a>首页</a></li>');
			_this.pagerPrev = $('<li class="pager-item" tg="prev" ><a><</a></li>');
			_this.pagerLeftQuick = $('<li class="pager-item" tg="leftquick" ><a>···</a></li>');
			_this.pagerNums = $('<li class="pager-nums"></li>');
			_this.pagerRightQuick = $('<li class="pager-item" tg="rightquick" ><a>···</a></li>');
			_this.pagerNext = $('<li class="pager-item" tg="next" ><a>></a></li>');
			_this.pagerLst = $('<li class="pager-item" tg="last" ><a>尾页</a></li>');
			_this.pagerJump = $('<li class="pager-jump" tg="jump" >跳转<input class="text" type="text"><input class="btn" type="button" value="GO"></li>');
			_this.pagerAll = $('<li class="pager-all" tg="all" >共<span id="allCnt"></span>条记录 共<span id="allPages"></span>页</li>');

			_this.pagerCon.append(_this.pagerSt);
			_this.pagerCon.append(_this.pagerPrev);
			_this.pagerCon.append(_this.pagerLeftQuick);
			_this.pagerCon.append(_this.pagerNums);
			_this.pagerCon.append(_this.pagerRightQuick);
			_this.pagerCon.append(_this.pagerNext);
			_this.pagerCon.append(_this.pagerLst);
			_this.pagerCon.append(_this.pagerJump);
			_this.pagerCon.append(_this.pagerAll);
			
		},
		gen:function(pageInfo){
			var _this = this;
			if(pageInfo && pageInfo.all && pageInfo.all > 0)
			{
				if(!_this.pagerCon){
					this.schema();
					Grid.tbpager.html("");
					Grid.tbpager.append(_this.pagerCon);
					_this.schemaEvents();
				}
				var allPages = pageInfo.all / pageInfo.pagesize;
				allPages = parseInt(allPages);
				if(pageInfo.all % pageInfo.pagesize > 0){
					allPages += 1;
				}
				pageInfo.allPages = allPages;
				_this.pageInfo = pageInfo;
				_this.setDispStatus(pageInfo);
				_this.refreshNumbers(pageInfo);
				_this.refreshAllCnt(pageInfo);
			}
		},
		
		setDispStatus:function(pageInfo){
			var _this = this;

			var steps = pageInfo.page / pageInfo.dispNumCnt;
			steps = parseInt(steps);

			if(pageInfo.withQuickKey){
				_this.pagerLeftQuick.removeClass("hide");
				_this.pagerRightQuick.removeClass("hide");

				if(steps == 0){
					_this.pagerLeftQuick.addClass("hide");
				}

				var nextQuickStart = (steps + 1 ) * pageInfo.dispNumCnt + 1; 
				if(nextQuickStart > pageInfo.allPages){
					_this.pagerRightQuick.addClass("hide");
				}

			}else{
				_this.pagerLeftQuick.addClass("hide");
				_this.pagerRightQuick.addClass("hide");
			}

			if(pageInfo.allPages > 1){
				if(pageInfo.withJump){
					_this.pagerJump.removeClass("hide");
				}else{
					_this.pagerJump.addClass("hide");
				}
			}else{
				_this.pagerJump.addClass("hide");
			}

			if(pageInfo.page == 1){
				_this.pagerPrev.addClass("hide");
				_this.pagerSt.addClass("hide");
			}else{
				_this.pagerPrev.removeClass("hide");
				_this.pagerSt.removeClass("hide");
			}

			if(pageInfo.page == pageInfo.allPages){
				_this.pagerNext.addClass("hide");
				_this.pagerLst.addClass("hide");
			}else{
				_this.pagerNext.removeClass("hide");
				_this.pagerLst.removeClass("hide");
			}
		},

		refreshNumbers:function(pageInfo){
			var _this = this;

			var step = pageInfo.page / pageInfo.dispNumCnt;
			step = parseInt(step);
			if(pageInfo.page % pageInfo.dispNumCnt == 0){
				step -= 1;
			}

			var start = step * pageInfo.dispNumCnt + 1;
			var end =  (step + 1) * pageInfo.dispNumCnt;
			if(end > pageInfo.allPages){
				end = pageInfo.allPages;
			}
			var str = '<ul >';
			for(var i=start;i<=end;i++){
				str += '<li class="' + ( i==pageInfo.page?'cur':'' ) + '" ><a>'+ i +'</a></li>'
			}
			str += '</ul>';
			_this.pagerNums.html(str);
			_this.numberEvents();
		},

		refreshAllCnt:function(pageInfo){
			var _this = this;
			_this.pagerAll.find("#allCnt").text(pageInfo.all);
			_this.pagerAll.find("#allPages").text(pageInfo.allPages);
		},

		numberEvents:function(){
			var _this = this;
			_this.pagerNums.find("li").on("click",function(){
				if($(this).hasClass("cur"))
					return;
				Grid.on_pager_change($(this).children().text());
			});
		},

		schemaEvents:function(){
			var _this = this;

			_this.pagerSt.on("click",function(){
				Grid.on_pager_change(1);
			});

			_this.pagerPrev.on("click",function(){
				var prev = _this.pageInfo.page - 1;
				Grid.on_pager_change(prev);
			});

			_this.pagerLeftQuick.on("click",function(){
				var step = _this.pageInfo.page / _this.pageInfo.dispNumCnt;
				step = parseInt(step);
				step -= 1;
				var prev = (step * _this.pageInfo.dispNumCnt) + 1;
				Grid.on_pager_change(prev);
			});

			_this.pagerRightQuick.on("click",function(){
				var step = _this.pageInfo.page / _this.pageInfo.dispNumCnt;
				step = parseInt(step);
				step += 1;
				var prev = (step * _this.pageInfo.dispNumCnt) + 1;
				Grid.on_pager_change(prev);
			});

			_this.pagerNext.on("click",function(){
				var next = _this.pageInfo.page + 1;
				Grid.on_pager_change(next);
			});

			_this.pagerLst.on("click",function(){
				Grid.on_pager_change(_this.pageInfo.allPages);
			});

			_this.pagerJump.find(".btn").on("click",function(){
				var number = _this.pagerJump.find(".text").val();
				try{
					if(!number || number == '0'){
						_this.pagerJump.find(".text").val("");
						return;
					}
					number = parseInt(number);
					if(number > _this.pageInfo.allPages){
						_this.pagerJump.find(".text").val(_this.pageInfo.page);
					}else{
						if(number != _this.pageInfo.page)
						{
							Grid.on_pager_change(number);
						}
					}
				}catch(e){

				}
			});




		}
	};
}

if("function"==typeof define && define.amd && define){
	define(['jquery','common/pccommons'],function($,pcCommon){
		return Grid($,pcCommon);
	});
}